#include "../../../include/View/Input/EasyXInput.h"
#include "../../../include/Core/Application.h"
#include <cstdio>

bool EasyXInput::init(Input* input) {
    EasyXInput* easyxinput = dynamic_cast<EasyXInput*>(input);
    return true;
}
bool EasyXInput::isKeyPressed(ExMessage* msg,Player*& player) {
    if (msg->message == WM_KEYDOWN) {
        //test
        //printf("keydown\n");
        printf("pos(%d,%d)\n",player->GetPos().x,player->GetPos().y);
        //endtest
        switch (msg->vkcode) {
            //仅支持英文输入法
            case VK_ESCAPE:
                printf("esc\n");
                break;
            case VK_SPACE:
                printf("space\n");
                break;
            case VK_UP:
            case 'W':
                player->move(1);
                break;
            case VK_DOWN:
            case 'S':
                player->move(2);
                break;
            case VK_LEFT:
            case 'A':
                player->move(3);
                break;
            case VK_RIGHT:
            case 'D':
                player->move(4);
                break;
            default:
                break;
        }
        return true;
    }
    if (msg->message == WM_KEYUP) {
        //test
        //printf("keyup\n");
        //endtest
    }
    return false;
}
bool EasyXInput::isMousePressed(ExMessage* msg) {
    if (msg->message == WM_LBUTTONDOWN) {
        //test
        printf("mouse down\n");
        //endtest
        return true;
    }
    if (msg->message == WM_LBUTTONUP) {
        //test
        printf("mouse up\n");
        //endtest
    }
    return false;
}
std::pair<int,int> EasyXInput::getMousePos() {
    if (msg.message == WM_MOUSEMOVE) {
        windowPos.first = msg.x;
        windowPos.second = msg.y;
        //test
        //printf("x:%hu y:%hu in window\n",windowPos.first,windowPos.second);
        //endtest
    }
    return EasyXInput::windowPos;
}
void EasyXInput::handleEvents(Player*& player) {
    winWidth= getwidth();
    winHeight= getheight();
    while (easyx::peekmessage(&msg)) {
        //test
        //std::printf("获取消息\n");
        //endtest
        keyStates[msg.wParam] = EasyXInput::isKeyPressed(&msg,player);
        mouseStates[msg.lParam] = isMousePressed(&msg);
        windowPos = EasyXInput::getMousePos();
        if (msg.message == WM_MOUSEWHEEL) {
        }
    }
}

